BackForward

/*----------------------------------------\
| Capitalize the 1st letter of a sentence or the 1st letter of each  |
| word in a setence; and parse a string variable;                    |
|-------------------------------------------|
|--------------------------------------------------------------------|
|---------------------------|
| indata: input data set;                                            |
| var: the variable need to be capitalized;                          |
| outdata: if an outdata is given, then the output the dataset to it;|
|          otherwise saved the dataset to the input dataset;         |
| dlm: the delimiter to separate word or setence;                    |
|      if dlm=' ' or WORD, capitalize the 1st letter of each word in |
|                          the variable;                             |
|      if dlm=';', ',' or SENTENCE, capitalize the 1st letter of the |
|                          sentence;                                 |
|      if dlm=any punctuation, capitalize the 1st letter after it;   |
|-----------------------------|
|--------------------------------------------------------------------|
|---------------------------------------|
| Example:                                                           |
| data one;                                                          |
|    input x y cpevent $21.;                                         |
|    datalines;                                                      |
| 1.1 1.2 'OneTwo'                                                   |
| 12  132 tHIS is, AN,EXAMPLE                                        |
| 1.1 2.3 1 MONTH FOLLOW-UP                                          |
| 2.1 232 4 MONTH FOLLOW-UP                                          |
| 1.3 4.1 7 MONTH FOLLOW-UP                                          |
| 4.2 100 UNSCHEDULED FOLLOW-UP                                      |
| ;                                                                  |
| data two;                                                          |
|   set one;                                                         |
|   cpevent=lowcase(cpevent);                                        |
|   dvstr(var=cpevent,                                               |
|             dlm=',' ';' '/' '\' '"' "'" '(' ')' ' ' '-',           |
|             except=('an', 'a'));                                   |
| run; %print(two);                                                  |
| Usage: dvstr(var=, dlm=' ');                                       |
\----------------------------------------*/
%macro dvstr(var=, dlm=' ', except='')/parmbuff;
/*--------------------------------------------\
| Copy Right: Duo Zhou;                        |
| Created: 12-27-2001 12:21am;                |
| Purpose: Capitalize the 1st letter of a     |
|          sentence or the 1st letter of each |
|          word in a setence;                 |
\--------------------------------------------*/
%local _dvupi_ _argtmp_;
%if (%index(%BQUOTE(&syspbuff), %str(%()) and %index(%BQUOTE(&syspbuff), %str(%())<2) %then
   %let syspbuff=%substr(%quote(%trim(%left(&syspbuff))), 2, %eval(%length(%quote(%trim(%left(&syspbuff))))-2));
%let syspbuff=%sysfunc(translate(%quote(&syspbuff), %str(%'), %str(%")));
%let syspbuff=%sysfunc(tranwrd(%quote(&syspbuff), ',', ÌÎÍ));
%let _dvupi_=0;
%do %while(%length(%nrbquote(%qscan(%nrbquote(&syspbuff), %eval(&_dvupi_+1), %str(,)))));
   %let _dvupi_=%eval(&_dvupi_+1);
   %let _argtmp_=%qscan(%nrbquote(&syspbuff), &_dvupi_, %str(,));
   %let _argtmp_=%sysfunc(tranwrd(%nrbquote(&_argtmp_), ÌÎÍ, ','));
   %if (not %index(%BQUOTE(&_argtmp_), %str(=))) %then %do;
      %if (%quote(&_dvupi_) eq 1) and (%length(%trim(%quote(%left(%quote(&var))))) le 2) %then %let var=&_argtmp_;
      %if (%quote(&_dvupi_) eq 2) and (%length(%trim(%quote(%left(%quote(&dlm))))) le 2) %then %let dlm=&_argtmp_;
      %if (%quote(&_dvupi_) eq 3) and (%length(%trim(%quote(%left(%quote(&except))))) le 2) %then %let except=&_argtmp_;
   %end;
%end;
length _last1char_ _last2char_ _dvchar_ $1.;
_NEWCP_=&var||&var;
%if (%index(%BQUOTE(%trim(%BQUOTE(%left(%BQUOTE(&except))))), %str(%()) eq 1) and
          (%index(%BQUOTE(%trim(%BQUOTE(%left(%BQUOTE(%sysfunc(reverse(&except))))))), %str(%))) eq 1)
   %then %let except=%substr(%quote(%trim(%left(&except))), 2, %eval(%length(%quote(%trim(%left(&except))))-2));
%let except=%sysfunc(translate(%quote(&except), %str(%'), %str(%"))); %let _excpti_=0; %let _excptcnt_=0;
&var=trimn(left(compbl(&var)));
%do %while(%length(%nrbquote(%qscan(%nrbquote(&except), %eval(&_excpti_+1), %str(,)))));
   %let _excpti_=%eval(&_excpti_+1);
   %let _excptmp_=%qscan(%nrbquote(&except), &_excpti_, %str(,));
   %if (%quote(%sysfunc(dequote(%quote(&_excptmp_)))) ne) %then %do;
      %let _excptmp_=%sysfunc(tranwrd(%nrbquote(&_excptmp_), ÌÎÍ, ','));
      %let _excptfr&_excpti_=%sysfunc(dequote(&_excptmp_));
      %let _excptto&_excpti_=®%trim(%left(&_excpti_));
      &var=tranwrd(compbl(&var), "&&_excptfr&_excpti_", "&&_excptto&_excpti_");
   %end;
   %else %let _excptfr&_excpti_=;
%end; %let _excptcnt_=&_excpti_;
%if (%quote(&dlm) eq ) %then %let dlm=' ';
   _last1char_=' '; _last2char_=' '; _NEWCP_=' ';
   _dummyqcp_=trimn(left(compress(&var, '"')));
   _dummyqcp_=trimn(left(compress(_dummyqcp_, "'")));
   _dummyqn_=length(trimn(left(&var)))-length(trimn(left(_dummyqcp_)));
   %if (%length(&dlm)>1) %then %do;
      retain _dummyqi_; _dummyqi_=0;
      do _dvari_=1 to length(&var);
         _dvchar_=substr(&var,_dvari_,1);
         if ((_last1char_ in (&dlm)) or (_last1char_=' ' and _last2char_ in (&dlm)) or (_dvari_=1)) then
            _dvchar_=upcase(_dvchar_);
         else _dvchar_=lowcase(_dvchar_);
         if (_last1char_ in (' ', '`', '~', '!', '@','#','$', '%', '^', '&','*','(',')','-','_','+','=','[',']','{','}','|','\',';',':','"',"'",'<','>',',','.','/','?')
            or (_last2char_ in (' ', '`', '~', '!', '@','#','$', '%', '^', '&','*','(',')','-','_','+','=','[',']','{','}','|','\',';',':','"',"'",'<','>',',','.','/','?'))
            or (_dvchar_ in (' ', '`', '~', '!', '@','#','$', '%', '^', '&','*','(',')','-','_','+','=','[',']','{','}','|','\',';',':','"',"'",'<','>',',','.','/','?'))) then do;
            if (_dvchar_ in ('`', '~', '!', '@','#','%', '^', ')','-','_','+','=',']','}','\',';',':','<','>',',','.','/','?')) then
               _NEWCP_=trimn(left(_NEWCP_))||_dvchar_;
            else if (_dvchar_ in ('$', '&','(','[','{','|')) then
               _NEWCP_=compbl(_NEWCP_)||_dvchar_;
            else if (_last1char_ in ('!','%', '&',')',']','}',';',':',',','.','?')) then
               _NEWCP_=compbl(_NEWCP_)||_dvchar_;
            else if (_last1char_=' ' and _last2char_ in ('`','~','@','#','$','^','*','(','[','{','\','<','>','/')) then
               _NEWCP_=trimn(left(_NEWCP_))||_dvchar_;
            else if (_last2char_ in ('!', '%', '&',')',']','}',';',':',',','.','?') and _last1char_=' ') then
               _NEWCP_=compbl(_NEWCP_)||_dvchar_;
            else if (_dvchar_ in ('"', "'")) then do;
               _dummyqi_=_dummyqi_+1;
               if (mod(_dummyqn_, 2)) then _NEWCP_=trimn(left(_NEWCP_))||_dvchar_;
               else if (mod(_dummyqi_, 2)) then _NEWCP_=compbl(_NEWCP_)||_dvchar_;
               else _NEWCP_=trimn(left(_NEWCP_))||_dvchar_;
               /* put '5 ' _dummyqi_= _NEWCP_=; */
            end;
            else if (_last1char_ ne ' ' and _dvchar_ ne ' ') then _NEWCP_=trimn(left(_NEWCP_))||_dvchar_;
            else _NEWCP_=compbl(_NEWCP_)||_dvchar_;
         end;
         else _NEWCP_=trimn(left(_NEWCP_))||_dvchar_;
         _last2char_=_last1char_; _last1char_=_dvchar_;
      end;
   %end;
   %else _NEWCP_=upcase(substr(&var,1,1))||lowcase(substr(&var,2,(length(&var)-1)));;
   %if (%quote(&_excptcnt_) ge 1) %then %do;
      %do _excpti_=1 %to &_excptcnt_;
         %if (%quote(&&_excptfr&_excpti_) ne) %then
            _NEWCP_=tranwrd(_NEWCP_, "&&_excptto&_excpti_", "&&_excptfr&_excpti_");;
      %end;
   %end;
   &var=_NEWCP_;
   drop _NEWCP_ _dvari_ _dvchar_ _last1char_ _last2char_ _dummyqn_ _dummyqi_ _dummyqcp_;
%mend dvstr;